package com.cnblog.blog.parsexml;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParseXML extends ParseXML {

	@Override
	public void RecommendPageParseXML(InputStream is, JSONObject jsonObject) throws JSONException {
		JSONArray jsonArray = new JSONArray();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(is);
			Element element = document.getDocumentElement();

			NodeList nodeList = element.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element bookElement = (Element) nodeList.item(i);
				String name = nodeList.item(i).getNodeName();
				String value = "";
				if (nodeList.item(i).getFirstChild() != null) {
					value = nodeList.item(i).getFirstChild().getNodeValue();
				}
				
				if (name.equalsIgnoreCase("title")) {
					jsonObject.putOpt("title", value);
				} else if (name.equalsIgnoreCase("id")) {
					jsonObject.putOpt("id", value);
				} else if (name.equalsIgnoreCase("updated")) {
					jsonObject.putOpt("updated", value);
				} else if (name.equalsIgnoreCase("entry")) {
					NodeList childNodes = bookElement.getChildNodes();
					JSONObject object = new JSONObject();
					for (int j = 0; j < childNodes.getLength(); j++) {
						Element nodeElement = (Element) childNodes.item(j);
						String childName = childNodes.item(j).getNodeName();
						if (childName.equalsIgnoreCase("link")) {
							String rel = nodeElement.getAttribute("rel");
							String href = nodeElement.getAttribute("href");

							JSONObject _object = new JSONObject();
							_object.putOpt("rel", rel);
							_object.putOpt("href", href);

							object.putOpt("link", _object);
						} else {
							String childValue = "";
							if (childNodes.item(j).getFirstChild() != null) {
								childValue = childNodes.item(j).getFirstChild().getNodeValue();
							}
							
							if (childName.equalsIgnoreCase("id")) {
								object.putOpt("id", childValue);
							} else if (childName.equalsIgnoreCase("title")) {
								object.putOpt("title", childValue);
							} else if (childName.equalsIgnoreCase("updated")) {
								object.putOpt("updated", childValue);
							} else if (childName.equalsIgnoreCase("blogapp")) {
								object.putOpt("blogapp", childValue);
							} else if (childName.equalsIgnoreCase("avatar")) {
								object.putOpt("avatar", childValue);
							} else if (childName.equalsIgnoreCase("postcount")) {
								object.putOpt("postcount", childValue);
							}
						}
					}
					jsonArray.put(object);
					jsonObject.put("entry", jsonArray);
				}
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void HoursTopViewPost(InputStream is, JSONObject jsonObject) throws JSONException {
		// TODO Auto-generated method stub
		JSONArray jsonArray = new JSONArray();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(is);
			Element element = document.getDocumentElement();

			NodeList nodeList = element.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element bookElement = (Element) nodeList.item(i);
				String name = nodeList.item(i).getNodeName();
				String value = "";
				if (nodeList.item(i).getFirstChild() != null) {
					value = nodeList.item(i).getFirstChild().getNodeValue();
				}
				
				if (name.equalsIgnoreCase("title")) {
					jsonObject.putOpt("title", value);
				} else if (name.equalsIgnoreCase("id")) {
					jsonObject.putOpt("id", value);
				} else if (name.equalsIgnoreCase("updated")) {
					jsonObject.putOpt("updated", value);
				} else if (name.equalsIgnoreCase("entry")) {
					NodeList childNodes = bookElement.getChildNodes();
					JSONObject object = new JSONObject();
					for (int j = 0; j < childNodes.getLength(); j++) {
						Element nodeElement = (Element) childNodes.item(j);
						String childName = childNodes.item(j).getNodeName();
						if (childName.equalsIgnoreCase("link")) {
							String rel = nodeElement.getAttribute("rel");
							String href = nodeElement.getAttribute("href");

							JSONObject _object = new JSONObject();
							_object.putOpt("rel", rel);
							_object.putOpt("href", href);

							object.putOpt("link", _object);
						} else {
							String childValue = "";
							if (childNodes.item(j).getFirstChild() != null) {
								childValue = childNodes.item(j).getFirstChild().getNodeValue();
							}
							
							if (childName.equalsIgnoreCase("id")) {
								object.putOpt("id", childValue);
							} else if (childName.equalsIgnoreCase("title")) {
								object.putOpt("title", childValue);
							} else if (childName.equalsIgnoreCase("published")) {
								object.putOpt("published", childValue);
							} else if (childName.equalsIgnoreCase("updated")) {
								object.putOpt("updated", childValue);
							} else if (childName.equalsIgnoreCase("diggs")) {
								object.putOpt("diggs", childValue);
							} else if (childName.equalsIgnoreCase("views")) {
								object.putOpt("views", childValue);
							} else if (childName.equalsIgnoreCase("comments")) {
								object.putOpt("comments", childValue);
							} else if (childName.equalsIgnoreCase("summary ")) {
								object.putOpt("summary ", childValue);
							} else if (childName.equalsIgnoreCase("author")) {
								NodeList secondChildNodes = nodeElement.getChildNodes();
								JSONObject secondObject = new JSONObject();
								for (int m = 0; m < secondChildNodes.getLength(); m++) {
									String secondChildName = secondChildNodes.item(m).getNodeName();
									String secondChildValue = "";
									if (secondChildNodes.item(m).getFirstChild() != null) {
										 secondChildValue = childNodes.item(m).getFirstChild().getNodeValue();
									}
									
									if (secondChildName.equalsIgnoreCase("name")) {
										secondObject.putOpt("name",secondChildValue);
									} else if (secondChildName.equalsIgnoreCase("uri")) {
										secondObject.putOpt("uri", secondChildValue);
									} else if (secondChildName.equalsIgnoreCase("avatar")) {
										secondObject.putOpt("avatar", secondChildValue);
									}
								}
								object.putOpt("author", secondObject);
							}
						}
					}
					jsonArray.put(object);
					jsonObject.put("entry", jsonArray);
				}
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void CommendsParseXML(InputStream is, JSONObject jsonObject) throws JSONException {
		JSONArray jsonArray = new JSONArray();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(is);
			Element element = document.getDocumentElement();

			NodeList nodeList = element.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element bookElement = (Element) nodeList.item(i);
				String name = nodeList.item(i).getNodeName();
				String value = "";
				if (nodeList.item(i).getFirstChild() != null) {
					value = nodeList.item(i).getFirstChild().getNodeValue();
				}
				
				if (name.equalsIgnoreCase("title")) {
					jsonObject.putOpt("title", value);
				} else if (name.equalsIgnoreCase("id")) {
					jsonObject.putOpt("id", value);
				} else if (name.equalsIgnoreCase("updated")) {
					jsonObject.putOpt("updated", value);
				} else if (name.equalsIgnoreCase("entry")) {
					NodeList childNodes = bookElement.getChildNodes();
					JSONObject object = new JSONObject();
					for (int j = 0; j < childNodes.getLength(); j++) {
						Element nodeElement = (Element) childNodes.item(j);
						String childName = childNodes.item(j).getNodeName();
						String childValue = "";
						if (childNodes.item(j).getFirstChild() != null) {
							childValue = childNodes.item(j).getFirstChild().getNodeValue();
						}
						
						if (childName.equalsIgnoreCase("id")) {
							object.putOpt("id", childValue);
						} else if (childName.equalsIgnoreCase("title")) {
							object.putOpt("title", childValue);
						} else if (childName.equalsIgnoreCase("published")) {
							object.putOpt("published", childValue);
						} else if (childName.equalsIgnoreCase("updated")) {
							object.putOpt("updated", childValue);
						} else if (childName.equalsIgnoreCase("content")) {
							object.putOpt("content", childValue);
						} else if (childName.equalsIgnoreCase("author")) {
							NodeList secondChildNodes = nodeElement.getChildNodes();
							JSONObject secondObject = new JSONObject();
							for (int m = 0; m < secondChildNodes.getLength(); m++) {
								String secondChildName = secondChildNodes.item(m).getNodeName();
								String secondChildValue = "";
								if (secondChildNodes.item(m).getFirstChild() != null) {
									secondChildValue = secondChildNodes.item(m).getFirstChild().getNodeValue();
								}
								
								if (secondChildName.equalsIgnoreCase("name")) {
									secondObject.putOpt("name", secondChildValue);
								} else if (secondChildName.equalsIgnoreCase("uri")) {
									secondObject .putOpt("uri", secondChildValue);
								}
							}
							object.putOpt("author", secondObject);
						}
					}
					jsonArray.put(object);
					jsonObject.put("entry", jsonArray);
				}
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void BloggerBodyParseXML(InputStream is, JSONObject jsonObject)
			throws JSONException {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(is);
			Element element = document.getDocumentElement();
			System.out.print(element.getNodeName());
			String name = element.getNodeName();

			System.out.print("name : " + name);
			if (name.equals("string")) {
				String value = element.getFirstChild().getNodeValue();
				jsonObject.putOpt("string", value);
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void SiteHomeRecentParseXML(InputStream is, JSONObject jsonObject)
			throws JSONException {
		JSONArray jsonArray = new JSONArray();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(is);
			Element element = document.getDocumentElement();

			NodeList nodeList = element.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element bookElement = (Element) nodeList.item(i);
				String name = nodeList.item(i).getNodeName();
				System.out.println("name : " + name);
				if (name.equalsIgnoreCase("link")) {
					String href = bookElement.getAttribute("href");
					jsonObject.putOpt("link", href);
				} else {
					String value = "";
					if (nodeList.item(i).getFirstChild() != null) {
						value = nodeList.item(i).getFirstChild().getNodeValue();
					}
					
					if (name.equalsIgnoreCase("title")) {
						jsonObject.putOpt("title", value);
					} else if (name.equalsIgnoreCase("id")) {
						jsonObject.putOpt("id", value);
					} else if (name.equalsIgnoreCase("updated")) {
						jsonObject.putOpt("updated", value);
					} else if (name.equalsIgnoreCase("entry")) {
						NodeList childNodes = bookElement.getChildNodes();
						JSONObject object = new JSONObject();
						for (int j = 0; j < childNodes.getLength(); j++) {
							Element nodeElement = (Element) childNodes.item(j);
							String childName = childNodes.item(j).getNodeName();

							if (childName.equalsIgnoreCase("link")) {
								String rel = nodeElement.getAttribute("rel");
								String href = nodeElement.getAttribute("href");

								JSONObject _object = new JSONObject();
								_object.putOpt("rel", rel);
								_object.putOpt("href", href);

								object.putOpt("link", _object);
							} else {
								String childValue = "";
								if (childNodes.item(j).getFirstChild() != null) {
									childValue = childNodes.item(j).getFirstChild().getNodeValue();
								}
								
								if (childName.equalsIgnoreCase("id")) {
									object.putOpt("id", childValue);
								} else if (childName.equalsIgnoreCase("title")) {
									object.putOpt("title", childValue);
								} else if (childName.equalsIgnoreCase("published")) {
									object.putOpt("published", childValue);
								} else if (childName.equalsIgnoreCase("updated")) {
									object.putOpt("updated", childValue);
								} else if (childName.equalsIgnoreCase("summary")) {
									object.putOpt("summary", childValue);
								} else if (childName.equalsIgnoreCase("author")) {
									NodeList secondChildNodes = nodeElement.getChildNodes();
									JSONObject secondObject = new JSONObject();
									for (int m = 0; m < secondChildNodes.getLength(); m++) {
										String secondChildName = secondChildNodes.item(m).getNodeName();
										String secondChildValue = "";
										if (secondChildNodes.item(m).getFirstChild() != null) {
											secondChildValue = secondChildNodes.item(m).getFirstChild().getNodeValue();
										}
										
										if (secondChildName.equalsIgnoreCase("name")) {
											secondObject.putOpt("name",secondChildValue);
										} else if (secondChildName.equalsIgnoreCase("uri")) {
											secondObject.putOpt("uri",secondChildValue);
										} else if (secondChildName.equalsIgnoreCase("avatar")) {
											secondObject.putOpt("avatar",secondChildValue);
										}
									}
									object.putOpt("author", secondObject);
								} else if (childName.equalsIgnoreCase("blogapp")) {
									object.putOpt("blogapp", childValue);
								} else if (childName.equalsIgnoreCase("diggs")) {
									object.putOpt("diggs", childValue);
								} else if (childName.equalsIgnoreCase("views")) {
									object.putOpt("views", childValue);
								} else if (childName.equalsIgnoreCase("comments")) {
									object.putOpt("comments", childValue);
								}
							}
						}
						jsonArray.put(object);
						jsonObject.put("entry", jsonArray);
					}
				}

			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void TenDaysTopDiggPostsParseXML(InputStream is,
			JSONObject jsonObject) throws JSONException {
		// TODO Auto-generated method stub
		JSONArray jsonArray = new JSONArray();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(is);
			Element element = document.getDocumentElement();

			NodeList nodeList = element.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element bookElement = (Element) nodeList.item(i);
				String name = nodeList.item(i).getNodeName();
				String value = "";
				if (nodeList.item(i).getFirstChild() != null) {
					value = nodeList.item(i).getFirstChild().getNodeValue();
				}
				
				if (name.equalsIgnoreCase("title")) {
					jsonObject.putOpt("title", value);
				} else if (name.equalsIgnoreCase("id")) {
					jsonObject.putOpt("id", value);
				} else if (name.equalsIgnoreCase("updated")) {
					jsonObject.putOpt("updated", value);
				} else if (name.equalsIgnoreCase("entry")) {
					NodeList childNodes = bookElement.getChildNodes();
					JSONObject object = new JSONObject();
					for (int j = 0; j < childNodes.getLength(); j++) {
						Element nodeElement = (Element) childNodes.item(j);
						String childName = childNodes.item(j).getNodeName();
						if (childName.equalsIgnoreCase("link")) {
							String rel = nodeElement.getAttribute("rel");
							String href = nodeElement.getAttribute("href");

							JSONObject _object = new JSONObject();
							_object.putOpt("rel", rel);
							_object.putOpt("href", href);

							object.putOpt("link", _object);
						} else {
							String childValue = "";
							if (childNodes.item(j).getFirstChild() != null) {
								childValue = childNodes.item(j).getFirstChild().getNodeValue();
							}
							
							if (childName.equalsIgnoreCase("id")) {
								object.putOpt("id", childValue);
							} else if (childName.equalsIgnoreCase("title")) {
								object.putOpt("title", childValue);
							} else if (childName.equalsIgnoreCase("summary")) {
								object.putOpt("summary", childValue);
							} else if (childName.equalsIgnoreCase("published")) {
								object.putOpt("summary", childValue);
							} else if (childName.equalsIgnoreCase("updated")) {
								object.putOpt("updated", childValue);
							} else if (childName.equalsIgnoreCase("diggs")) {
								object.putOpt("diggs", childValue);
							} else if (childName.equalsIgnoreCase("views")) {
								object.putOpt("views", childValue);
							} else if (childName.equalsIgnoreCase("comments")) {
								object.putOpt("comments", childValue);
							} else if (childName.equalsIgnoreCase("author")) {
								NodeList secondChildNodes = nodeElement.getChildNodes();
								JSONObject secondObject = new JSONObject();
								for (int m = 0; m < secondChildNodes.getLength(); m++) {
									String secondChildName = secondChildNodes.item(m).getNodeName();
									String secondChildValue = "";
									if (secondChildNodes.item(m).getFirstChild() != null) {
										secondChildValue = secondChildNodes .item(m).getFirstChild().getNodeValue();
									}
									
									if (secondChildName.equalsIgnoreCase("name")) {
										secondObject.putOpt("name",secondChildValue);
									} else if (secondChildName.equalsIgnoreCase("uri")) {
										secondObject.putOpt("uri",secondChildValue);
									}
								}
								object.putOpt("author", secondObject);
							}
						}
					}
					jsonArray.put(object);
					jsonObject.put("entry", jsonArray);
				}
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void BlogSingleParseXML(InputStream is, JSONObject jsonObject)
			throws JSONException {
		JSONArray jsonArray = new JSONArray();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(is);
			Element element = document.getDocumentElement();

			NodeList nodeList = element.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Element bookElement = (Element) nodeList.item(i);
				String name = nodeList.item(i).getNodeName();
				String value = "";
				if (nodeList.item(i).getFirstChild() != null) {
					value = nodeList.item(i).getFirstChild().getNodeValue();
				}
				
				if (name.equalsIgnoreCase("title")) {
					jsonObject.putOpt("title", value);
				} else if (name.equalsIgnoreCase("id")) {
					jsonObject.putOpt("id", value);
				} else if (name.equalsIgnoreCase("updated")) {
					jsonObject.putOpt("updated", value);
				} else if (name.equalsIgnoreCase("logo")) {
					jsonObject.putOpt("logo", value);
				} else if (name.equalsIgnoreCase("author")) {
					NodeList secondChildNodes = bookElement.getChildNodes();
					JSONObject secondObject = new JSONObject();
					for (int m = 0; m < secondChildNodes.getLength(); m++) {
						String secondChildName = secondChildNodes.item(m).getNodeName();
						String secondChildValue = "";
						if (secondChildNodes.item(m).getFirstChild() != null) {
							secondChildValue = secondChildNodes.item(m).getFirstChild().getNodeValue();
						}
						
						if (secondChildName.equalsIgnoreCase("name")) {
							secondObject.putOpt("name", secondChildValue);
						} else if (secondChildName.equalsIgnoreCase("uri")) {
							secondObject.putOpt("uri", secondChildValue);
						} else if (secondChildName.equalsIgnoreCase("avatar")) {
							secondObject.putOpt("avatar", secondChildValue);
						}
					}
					jsonObject.putOpt("author", secondObject);
				} else if (name.equalsIgnoreCase("postcount")) {
					jsonObject.putOpt("postcount", value);
				} else if (name.equalsIgnoreCase("entry")) {
					NodeList childNodes = bookElement.getChildNodes();
					JSONObject object = new JSONObject();
					for (int j = 0; j < childNodes.getLength(); j++) {
						Element nodeElement = (Element) childNodes.item(j);
						String childName = childNodes.item(j).getNodeName();
						if (childName.equalsIgnoreCase("link")) {
							String rel = nodeElement.getAttribute("rel");
							String href = nodeElement.getAttribute("href");

							JSONObject _object = new JSONObject();
							_object.putOpt("rel", rel);
							_object.putOpt("href", href);

							object.putOpt("link", _object);
						} else {
							String childValue = "";
							if (childNodes.item(j).getFirstChild() != null) {
								childValue = childNodes.item(j).getFirstChild().getNodeValue();
							}
							
							if (childName.equalsIgnoreCase("id")) {
								object.putOpt("id", childValue);
							} else if (childName.equalsIgnoreCase("title")) {
								object.putOpt("title", childValue);
							} else if (childName.equalsIgnoreCase("summary")) {
								object.putOpt("summary", childValue);
							} else if (childName.equalsIgnoreCase("published")) {
								object.putOpt("published", childValue);
							} else if (childName.equalsIgnoreCase("updated")) {
								object.putOpt("updated", childValue);
							} else if (childName.equalsIgnoreCase("diggs")) {
								object.putOpt("diggs", childValue);
							} else if (childName.equalsIgnoreCase("views")) {
								object.putOpt("views", childValue);
							} else if (childName.equalsIgnoreCase("comments")) {
								object.putOpt("comments", childValue);
							} else if (childName.equalsIgnoreCase("author")) {
								NodeList secondChildNodes = nodeElement.getChildNodes();
								JSONObject secondObject = new JSONObject();
								for (int m = 0; m < secondChildNodes.getLength(); m++) {
									String secondChildName = secondChildNodes.item(m).getNodeName();
									String secondChildValue = "";
									if (secondChildNodes.item(m).getFirstChild() != null) {
										secondChildValue = secondChildNodes.item(m).getFirstChild().getNodeValue();
									}
									
									if (secondChildName.equalsIgnoreCase("name")) {
										secondObject.putOpt("name", secondChildValue);
									} else if (secondChildName.equalsIgnoreCase("uri")) {
										secondObject.putOpt("uri", secondChildValue);
									}
								}
								object.putOpt("author", secondObject);
							}
						}
					}
					jsonArray.put(object);
					jsonObject.put("entry", jsonArray);
				}
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
